package com.example.piece.ui.demo

import android.annotation.SuppressLint
import android.content.res.Configuration
import android.os.Bundle
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.databinding.ViewDataBinding
import com.therouter.router.Route
import com.example.piece.constant.RoutePath
import com.example.piece.databinding.TestActivityStatusBarUtilsInfoBinding
import com.example.piece.ui.activity.Layer3Activity
import com.gyf.immersionbar.ktx.*
import com.example.piece.R


/**
 * Bar相关参数信息（kotlin）
 */
@Route(path = RoutePath.TESTActivity.STATUS_BAR_UTILS_TEST_INfO_ACTIVITY)
class StatusBarUtilsTestInfoActivity :  Layer3Activity<TestActivityStatusBarUtilsInfoBinding, ViewDataBinding>() {
    override val isTransparentStatusBar: Boolean = true
    override val isLockPortrait: Boolean = true
    override val statusBarAlpha: Int = 0
    override val isDarkMode: Boolean = true
    override val isFitsSystemWindows: Boolean = false
    override val leftIcon1Res: Int = R.drawable.ic_back
    override val centerLayoutRes: Int = R.layout.test_activity_status_bar_utils_info
    override val title: String
    get() = "Bar相关参数信息（kotlin）"
    private var mIsHideStatusBar = false



    override fun onClickLeftIcon1(view: View) {
        onBackPressed()

    }

    override fun initData(savedInstanceState: Bundle?) {
        super.initData(savedInstanceState)
        initView()
        setListener()
    }
    @SuppressLint("SetTextI18n")
     fun initView() {
        mCenterViewBinding?.mTvStatus?.text = "${mCenterViewBinding?.mTvStatus?.title}$statusBarHeight".content()
        mCenterViewBinding?.mTvHasNav?.text = "${mCenterViewBinding?.mTvHasNav?.title}$hasNavigationBar".content()
        mCenterViewBinding?.mTvNav?.text = "${mCenterViewBinding?.mTvNav?.title}$navigationBarHeight".content()
        mCenterViewBinding?.mTvNavWidth?.text = "${mCenterViewBinding?.mTvNavWidth?.title}$navigationBarWidth".content()
        mCenterViewBinding?.mTvAction?.text = "${mCenterViewBinding?.mTvAction?.title}$actionBarHeight".content()
        mCenterViewBinding?.mTvHasNotch?.post { mCenterViewBinding?.mTvHasNotch?.text = "${mCenterViewBinding?.mTvHasNotch?.title}$hasNotchScreen".content() }
        mCenterViewBinding?.mTvNotchHeight?.post { mCenterViewBinding?.mTvNotchHeight?.text = "${mCenterViewBinding?.mTvNotchHeight?.title}$notchHeight".content() }
        mCenterViewBinding?.mTvFits?.text = "${mCenterViewBinding?.mTvFits?.title}${findViewById<View>(android.R.id.content).checkFitsSystemWindows}".content()
        mCenterViewBinding?.mTvStatusDark?.text = "${mCenterViewBinding?.mTvStatusDark?.title}$isSupportStatusBarDarkFont".content()
        mCenterViewBinding?.mTvNavigationDark?.text = "${mCenterViewBinding?.mTvNavigationDark?.title}$isSupportNavigationIconDark".content()
    }

    @SuppressLint("SetTextI18n")
     fun setListener() {
        mCenterViewBinding?.mBtnStatus?.setOnClickListener {
            mIsHideStatusBar = if (!mIsHideStatusBar) {
                hideStatusBar()
                true
            } else {
                showStatusBar()
                false
            }
        }
        mCenterViewBinding?.mTvInsets?.let {
            ViewCompat.setOnApplyWindowInsetsListener(it) { _, windowInsetsCompat ->
                mCenterViewBinding?.mTvInsets?.text = "${mCenterViewBinding?.mTvInsets?.title}${windowInsetsCompat.systemWindowInsetTop}".content()
                windowInsetsCompat.consumeSystemWindowInsets()
            }
        }
    }

    private fun String.content(): SpannableString {
        val split = split("   ")
        return SpannableString(this).apply {
            val colorSpan = ForegroundColorSpan(ContextCompat.getColor(this@StatusBarUtilsTestInfoActivity, R.color.dialogxColorBlue))
            setSpan(colorSpan, this.length - split[1].length, this.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
        }
    }

    private val TextView.title get() = text.toString().split("   ")[0] + "   "

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        initView()
    }
}